package com.ghn.cocknovel.ui.activity.map

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.example.basemodel.base.BaseActivity
import com.ghn.cocknovel.BR
import com.ghn.cocknovel.R
import com.ghn.cocknovel.databinding.ActivityNavigatBinding
import com.ghn.cocknovel.ui.activity.map.vm.MapViewModel
import com.tencent.map.fusionlocation.model.TencentGeoLocation
import com.tencent.navix.api.NavigatorZygote
import com.tencent.navix.api.layer.NavigatorLayerRootDrive
import com.tencent.navix.api.layer.NavigatorViewStub
import com.tencent.navix.api.location.GeoLocationObserver
import com.tencent.navix.api.location.LocationApi
import com.tencent.navix.api.model.NavCameraInfo
import com.tencent.navix.api.model.NavDayNightStatus
import com.tencent.navix.api.model.NavDriveDataInfo
import com.tencent.navix.api.model.NavDriveRoute
import com.tencent.navix.api.model.NavDriveRouteData
import com.tencent.navix.api.model.NavDriveRoutePlan
import com.tencent.navix.api.model.NavEnlargedMapInfo
import com.tencent.navix.api.model.NavError
import com.tencent.navix.api.model.NavGpsStatusInfo
import com.tencent.navix.api.model.NavIdleSectionInfo
import com.tencent.navix.api.model.NavLaneInfo
import com.tencent.navix.api.model.NavLocationInfo
import com.tencent.navix.api.model.NavParallelRoadStatus
import com.tencent.navix.api.model.NavRecommendRouteInfo
import com.tencent.navix.api.model.NavRoutePlan
import com.tencent.navix.api.model.NavRouteReqParam
import com.tencent.navix.api.model.NavSearchPoint
import com.tencent.navix.api.model.NavTTSInfo
import com.tencent.navix.api.model.NavTTSMode
import com.tencent.navix.api.model.NavTollStationInfo
import com.tencent.navix.api.model.NavTrafficJamInfo
import com.tencent.navix.api.model.NavWaypoint
import com.tencent.navix.api.navigator.NavigatorDrive
import com.tencent.navix.api.observer.NavigatorDriveObserver
import com.tencent.navix.api.observer.SimpleNavigatorDriveObserver
import com.tencent.navix.api.plan.DriveRoutePlanRequestCallback
import com.tencent.navix.api.plan.RoutePlanRequester
import com.tencent.navix.api.tts.NavigatorTTSPlayer
import com.tencent.navix.tts.DefaultTTSPlayer
import com.tencent.navix.tts.api.TTSPlayer
import com.tencent.navix.ui.NavigatorLayerViewDrive

class NavigatActivity : BaseActivity<ActivityNavigatBinding, MapViewModel>() {
    protected var navigatorDrive: NavigatorDrive? = null
    protected var layerRootDrive: NavigatorLayerRootDrive? = null
    protected var layerViewDrive: NavigatorLayerViewDrive? = null
    protected var locationApi: LocationApi? = null
    override fun initVariableId(): Int {
        return  BR.mode
    }

    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_navigat
    }

    override fun initParam() {
        // 创建驾车 NavigatorDrive
        navigatorDrive = NavigatorZygote.with(applicationContext).navigator<NavigatorDrive>(
            NavigatorDrive::class.java
        )
        // 创建导航地图层 NavigatorLayerRootDrive
        val navigatorViewStub = findViewById<NavigatorViewStub>(R.id.navigator_view_stub)
        navigatorViewStub.setTravelMode(NavRouteReqParam.TravelMode.TravelModeDriving)
        navigatorViewStub.inflate()
        layerRootDrive = navigatorViewStub.getNavigatorView<NavigatorLayerRootDrive>()
        // 创建默认面板 NavigatorLayerViewDrive，并添加到导航地图层
        layerViewDrive = NavigatorLayerViewDrive(this)
        layerRootDrive?.addViewLayer(layerViewDrive)

        // 将导航地图层绑定到Navigator
        navigatorDrive?.bindView(layerRootDrive)
        //显示或隐藏导航图层
        layerRootDrive?.view?.visibility=View.VISIBLE
        locationApi = navigatorDrive!!.locationApi()
        // 注册定位回调
        locationApi?.addLocationObserver(locationObserver, 3000)
        //注册导航监听
        navigatorDrive?.registerObserver(driveObserver)
        //任务详情页查询任务信息
        viewModel.queryTransitOrder()
        navigatorDrive?.registerObserver(object : NavigatorDriveObserver{
            override fun onDidStartNavigation() {

            }

            override fun onDidStopNavigation() {//结束导航
                finish()
            }

            override fun onWillArriveDestination() {

            }

            override fun onNavDataInfoUpdate(p0: NavDriveDataInfo?) {

            }

            override fun onNavLocationInfoUpdate(p0: NavLocationInfo?) {

            }

            override fun onStartRerouting(p0: Int) {

            }

            override fun onRouteRequestDidSucceed(p0: NavDriveRoutePlan?) {

            }

            override fun onRerouteDidSucceed(p0: Int, p1: NavDriveRoutePlan?) {

            }

            override fun onRerouteDidFail(p0: Int, p1: NavError?) {

            }

            override fun onPlayTTS(p0: NavTTSInfo?) {

            }

            override fun onGPSStatusChanged(p0: NavGpsStatusInfo?) {

            }

            override fun onTtsModeChange(p0: NavTTSMode?) {

            }

            override fun onDayNightStatusChange(p0: NavDayNightStatus?) {

            }

            override fun onShowEnlargedMap(p0: NavEnlargedMapInfo?) {

            }

            override fun onHideEnlargedMap() {

            }

            override fun onWillShowLaneGuide(p0: NavLaneInfo?) {

            }

            override fun onArriveWaypoint(p0: NavWaypoint?) {

            }

            override fun onHideLaneGuide() {

            }

            override fun onUpdateParallelRoad(p0: NavParallelRoadStatus?) {

            }

            override fun onEnterIdleSection(p0: NavIdleSectionInfo?) {

            }

            override fun onMainRouteDidChange(p0: String?, p1: Int) {

            }

            override fun onRecommendRouteInfo(p0: NavRecommendRouteInfo?) {

            }

            override fun onAddAlternativeRoutes(p0: MutableList<NavDriveRoute>?) {

            }

            override fun onDeleteAlternativeRoutes(p0: MutableList<String>?) {

            }

            override fun onUpdateRouteTraffic(p0: MutableList<NavDriveRouteData>?) {

            }

            override fun onRerouteDidInFence(p0: Int) {

            }

            override fun onTollStationInfoUpdate(p0: NavTollStationInfo?) {

            }

            override fun onTrafficJamInfoUpdate(p0: NavTrafficJamInfo?) {

            }

            override fun onCameraInfoUpdate(p0: MutableList<NavCameraInfo>?) {

            }

        })

    }
    var locationObserver: GeoLocationObserver = object : GeoLocationObserver() {
        @SuppressLint("SuspiciousIndentation")
        override fun onGeoLocationChanged(tencentGeoLocation: TencentGeoLocation) {
            super.onGeoLocationChanged(tencentGeoLocation)
            viewModel.queryPassengerBySite.observe(this@NavigatActivity) {
//                // 算路并开启导航
                navigatorDrive?.searchRoute(
                    RoutePlanRequester.Companion.newBuilder(NavRouteReqParam.TravelMode.TravelModeDriving)
                        .start(NavSearchPoint(tencentGeoLocation.location.latitude, tencentGeoLocation.location.longitude))
                        .end(NavSearchPoint(it?.siteLatitude!!, it.siteLongitude))
                        .build(),
                    DriveRoutePlanRequestCallback { navRoutePlan: NavRoutePlan<NavDriveRoute>?, error: NavError? ->
                        if (error != null) {
                            // handle error
                            return@DriveRoutePlanRequestCallback
                        }
                        if (navRoutePlan != null) {
                            val routePlanList =
                                navRoutePlan.routeDatas
                            if (routePlanList != null && routePlanList.size > 0) {
                                navigatorDrive?.startNavigation(navRoutePlan.routeDatas[0].getRouteId())
                            }
                        }
                    }
                )
                navigatorDrive!!.ttsPlayer = DefaultTTSPlayer()
            }
        }
    }
    override fun onStart() {
        super.onStart()
        layerRootDrive?.onStart()
    }

    override fun onRestart() {
        super.onRestart()
        layerRootDrive?.onRestart()
    }

    override fun onResume() {
        super.onResume()
        layerRootDrive?.onResume()
    }

    override fun onPause() {
        super.onPause()
        layerRootDrive?.onPause()
    }

    override fun onStop() {
        super.onStop()
        layerRootDrive?.onStop()
    }

    override fun onDestroy() {
        super.onDestroy()
        layerRootDrive?.onDestroy()
        // 移除导航监听
        navigatorDrive?.unregisterObserver(driveObserver)
        // 移除默认面板
        layerRootDrive?.removeViewLayer(layerViewDrive)
        // 解绑导航地图
        navigatorDrive?.unbindView(layerRootDrive)
        // 关闭TTS和导航
        val ttsPlayer: NavigatorTTSPlayer = navigatorDrive?.getTTSPlayer()!!
        if (ttsPlayer != null && ttsPlayer is TTSPlayer) {
            (ttsPlayer as TTSPlayer).stop()
        }
        navigatorDrive?.stopNavigation()
    }

    private val driveObserver: SimpleNavigatorDriveObserver =
        object : SimpleNavigatorDriveObserver() {
            override fun onWillArriveDestination() {
                super.onWillArriveDestination()
                if (navigatorDrive != null) {
                    navigatorDrive?.stopNavigation()
                }
            }
        }
}